<?php
/**
 * 提货
 * User: bgl
 * (model)
 */
namespace App\Models\ProductPickup;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class Pickup extends Model{

    protected $table = 'product_pickup';

    protected $guarded = [];

    /**
     * 地区
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    /**
     * sku
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    /**
     * classify
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * 品类下的sku
     * @param $classify_id
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getClassifySku($classify_id){
        $query = self::query();
        if ($classify_id) {
            $query->where('classify_id', $classify_id);
        }
        $query->groupBy('psku_id');

        return $query->get();
    }

    /**
     * 品类下的站点
     * @param $classify_id
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getClassifyArea($classify_id){
        $query = self::query();
        if ($classify_id) {
            $query->where('classify_id', $classify_id);
        }
        $query->groupBy('area_id');

        return $query->get();
    }

    /**
     * (品类)站点下的sku
     * @param $classify_id
     * @param $area_id
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getClassifyAreaSku($classify_id, $area_id){
        $query = self::query();
        if ($classify_id) {
            $query->where('classify_id', $classify_id);
        }
        if ($area_id) {
            $query->where('area_id', $area_id);
        }
        $query->groupBy('psku_id');

        return $query->get();
    }

}
